package controller

import (
	"fx_swift_boss/api"
	"fx_swift_boss/internal/service"
	"fx_swift_boss/pkg/code"
	"fx_swift_boss/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 新增/修改 子帐号并且加入权限
func AddBoss(c *gin.Context) {
	req := &api.AddBossReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewAuthSrv().AddBoss(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 子帐号列表
func GetAccountList(c *gin.Context) {
	result, err := service.NewAuthSrv().GetAccountList(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 新增权限
func AddAuth(c *gin.Context) {
	req := &api.AddAuthReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewAuthSrv().AddAuth(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 新增/更新/删除 角色对应的权限
func AddRoleAuth(c *gin.Context) {
	req := &api.AddRoleAuthReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewAuthSrv().AddRoleAuth(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 获取用户对应的权限和角色对应的权限
func GetUserAuth(c *gin.Context) {
	result, err := service.NewAuthSrv().GetUserAuth(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取用户对应的权限/角色对应的权限
func GetAuthToUserOrRole(c *gin.Context) {
	req := &api.GetAuthToUserOrRoleReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewAuthSrv().GetAuthToUserOrRole(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取所有角色
func GetRoleList(c *gin.Context) {
	result, err := service.NewAuthSrv().GetRoleList(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
